<template>
	<view class="bizPage">
		<status-bar :title="title" :fromShare="fromShare" />
		<view class="padding20">
			<view class="search bgwhite d-flex a-center m-bot20">
				<text class="iconfont icon-sousuo color-333 m-right20"></text>
				<input class="flex-1 size26" v-model="fileName" placeholder="请输入文件名搜索" confirm-type="search" @blur="getList()" />
			</view>
			<view class="card">
				<view class="item" @click="handleOpen(item)" v-for="(item,index) in list" :key="index">
					<image class="icon m-right20" :src="staticImg + matchIcon(item)" mode="aspectFit"></image>
					<view class="flex-1 size28 color-222 m-right40">{{ item.name }}</view>
					<text class="iconfont icon-more size40 color-999" @click.stop="openAction(item)"></text>
				</view>
			</view>

			<up-empty :show="noData" mode="list" marginTop="100"></up-empty>
		</view>
		
		<up-popup :show="showAction" :round="10" closeOnClickOverlay @close="showAction = false">
			<view class="action-item p-re" @click="selectType(item)" v-for="(item,index) in actionList" :key="index">
				<button class="btn-share" open-type="share" v-if="item.type == 'share' && canShare"></button>
				<text class="size32">{{ item.name }}</text>
			</view>  
		</up-popup>
	</view>
</template>

<script setup>
	import { ref } from 'vue'
	import { onLoad, onShareAppMessage } from '@dcloudio/uni-app'
	import { staticImg } from '@/utils/config.js'
	import { useMainStore } from '@/stores/index'
	import { Toast, imgUrls, showLoading, hideLoading } from '@/utils/utils.js'
	import Http from '@/utils/request.js'
	import statusBar from '../components/statusBar.vue'
	
	const mainStore = useMainStore()
	const dirId = ref('') // 文件夹id
	const title = ref('')
	const fileId = ref('') // 文件id
	const fileName = ref('')
	const fromShare = ref(0)
	const list = ref([])
	const noData = ref(false)

	onLoad((e) => {
		if(e.dirId) dirId.value = e.dirId
		if(e.title) title.value = e.title
		if(e.fileId) fileId.value = e.fileId
		if(e.fromShare) fromShare.value = e.fromShare
		getList()
	})

	const getList = () => {
		noData.value = false
		Http.post({
			url: '/hr/fileInfo/selectAllDocument',
			data: {
				dirId: dirId.value,
				fileId: fileId.value,
				fileName: fileName.value
			}
		}).then(res => {
			if(res.code != 0) return Toast(res.msg)
			let result = res.data || []
			list.value = result.filter(item => !item.flagDir) // 过滤掉文件夹
			if(!list.value.length) noData.value = true
		})
	}
	
	const pageData = ref('') // 当前文件对象
	const showAction = ref(false)
	const actionList = ref([
		{ name: '查看', type: 'view' },
		{ name: '转发', type: 'share' },
		{ name: '下载', type: 'download' }
	])
	const canShare = ref(false)
	
	const openAction = (item) => {
		pageData.value = item
		showAction.value = true
		canShare.value = item.permissions && item.permissions.includes('share') ? true : false
	}
	
	const selectType = (item) => {
		if(!pageData.value.permissions || !pageData.value.permissions.includes(item.type)) return Toast(`暂无${item.name}权限`)
		if(item.type == 'view') handleOpen(pageData.value)
		if(item.type == 'download') handleDownload(pageData.value)
	}
	
	// 查看
	const handleOpen = (item) => {
		if(!item.permissions || !item.permissions.includes('view')) return Toast('暂无查看权限')
		// 是否显示长按菜单
		// let showMenu = item.permissions.includes('share') && item.permissions.includes('download')
		if(item.flagImg) {
			uni.previewImage({
				urls: [imgUrls(item.url)],
				showmenu: false
			})
			return
		}
		showLoading()
		uni.downloadFile({
			url: item.url,
			success(res) {
				uni.openDocument({
					filePath: res.tempFilePath,
					showMenu: false,
					complete() {
						hideLoading()
					}
				})
			}
		})
	}
	
	// 下载
	const handleDownload = (item) => {
		showLoading()
		uni.downloadFile({
			url: item.url,
			success(res) {
				if(item.flagImg) { // 保存图片到系统相册
					uni.saveImageToPhotosAlbum({
						filePath: res.tempFilePath,
						complete() {
							hideLoading()
						}
					})
				} else {
					uni.openDocument({
						filePath: res.tempFilePath,
						complete() {
							hideLoading()
						}
					})
					// 内部存储/Android/data/com.tencent.mm/MicroMsg/wxanewfiles/...
					// wx.getFileSystemManager().saveFile({
					// 	tempFilePath: res.tempFilePath,
					// 	filePath: `${wx.env.USER_DATA_PATH}/hello.txt`,
					// 	complete() {
					// 		hideLoading()
					// 	}
					// })
				}
			}
		})
	}
	
	const matchIcon = (item) => {
		let suffix = getSuffix(item.url)
		if(item.suffix.startsWith('image')) {
			return 'picture.png'
		} else if(item.suffix.startsWith('text')) {
			return 'text.png'
		} else if(suffix == 'pdf') {
			return 'PDF.png'
		} else if(suffix == 'doc' || suffix == 'docx') {
			return 'word.png'
		} else if(suffix == 'xls' || suffix == 'xlsx') {
			return 'excel.png'
		} else if(suffix == 'ppt' || suffix == 'pptx') {
			return 'slide.png'
		} else {
			return 'unknown.png'
		}
	}
	
	const getSuffix = (url) => {
		let suffix = url.slice(url.lastIndexOf('.') + 1)
		return suffix.toLowerCase()
	}
	
	onShareAppMessage((res) => {
		if(res.from == 'button') {
			let { name, id } = pageData.value
			return {
			  	title: name,
				path: `/pages/index/index?Type=1&dirId=${dirId.value}&title=${title.value}&fileId=${id}`
			}
		}
		return {
		  title: mainStore.BaseInfo.siteName,
		  path: '/pages/index/index'
		}
	})
</script>

<style lang="scss" scoped>
	.bizPage {
		min-height: 100vh;
		background: #F9F9F9;
	}
	.card {
		background: #FFFFFF;
		border-radius: 20rpx;
		.item {
			padding: 30rpx 20rpx 30rpx 40rpx;
			display: flex;
			align-items: center;
			.icon {
				width: 48rpx;
				height: 48rpx;
			}
		}
	}
	.action-item {
		padding: 34rpx;
		text-align: center;
		color: #303133;
		&:not(:last-child) {
			border-bottom: 1rpx solid #f2f2f2;
		}
		.btn-share {
			position: absolute;
			width: 100%;
			height: 100%;
			top: 0;
			left: 0;
			opacity: 0;
			z-index: 1;
		}
	}
	.search {
		height: 66rpx;
		box-shadow: 0rpx 3rpx 4rpx 0rpx rgba(58,100,132,0.11);
		border-radius: 33rpx;
		padding: 0 20rpx;
	}
</style>